package net.xxcl.exam.service;

import java.util.*;

import net.xxcl.exam.mapper.PaperMapper;
import net.xxcl.exam.mapper.PaperQuestionMapper;
import net.xxcl.exam.mapper.QuestionMapper;
import net.xxcl.exam.pojo.Criteria;
import net.xxcl.exam.pojo.Paper;
import net.xxcl.exam.pojo.PaperQuestionKey;
import net.xxcl.exam.pojo.Question;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class PaperService {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private PaperQuestionMapper paperQuestionMapper;

    private static final Logger logger = LoggerFactory.getLogger(PaperService.class);

    @Transactional(readOnly = true)
    public int countByExample(Criteria example) {
        int count = this.paperMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Transactional(readOnly = true)
    public Paper selectByPrimaryKey(Integer id) {
        return this.paperMapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<Paper> selectByExample(Criteria example) {
        return this.paperMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return this.paperMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Paper record) {
        return this.paperMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Paper record) {
        return this.paperMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.paperMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Paper record, Criteria example) {
        return this.paperMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Paper record, Criteria example) {
        return this.paperMapper.updateByExample(record, example);
    }

    public int insert(Paper record) {
        return this.paperMapper.insert(record);
    }

    public int insertSelective(Paper record) {
        return this.paperMapper.insertSelective(record);
    }

    /**
     * 删除试卷
     * @param ids
     * @return
     */
    public int deletePaper(String ids) {
        String[] str = ids.split(",");
        for(String id:str){
            this.paperMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        return str.length;
    }

    /**
     * 添加试卷
     * @param paper
     * @param danxuan
     * @param duoxuan
     * @param panduan
     * @param wordCaozuo
     * @param excelCaozuo
     * @param powerpointCaozuo
     * @param dazi
     * @return
     */
    public int insertPaper(Paper paper,Integer danxuan,Integer duoxuan,Integer panduan,Integer wordCaozuo,
                           Integer excelCaozuo, Integer powerpointCaozuo, Integer dazi){
        int totalMark = 0;
        paper.setAddtime(new Date());
        paperMapper.insertSelective(paper);
        int paperId = paper.getId();
        Criteria criteria = new Criteria();
        Criteria questionCriteria = new Criteria();
        if(danxuan!=null){
            List<Integer> randomList = new ArrayList<Integer>();
            criteria.put("type",1);
            int total = questionMapper.countByExample(criteria);
            if(total<danxuan){
                throw new RuntimeException("您设置的单选题数量大于数据库已有的！");
            }
            for(int i=0;i<danxuan;i++){
                while (true){
                    Integer rand = (int)(Math.random()*total);
                    if(!randomList.contains(rand)){
                        randomList.add(rand);
                        break;
                    }
                }
            }
            Question qusetion = new Question();
            PaperQuestionKey paperQuestionKey = new PaperQuestionKey();
            paperQuestionKey.setPaperid(paper.getId());
            for(int t:randomList){
                criteria.clear();
                criteria.put("type",1);
                criteria.setMysqlOffset(t);
                criteria.setMysqlLength(1);
                qusetion = questionMapper.selectByExample(criteria).get(0);
                paperQuestionKey.setQuestionid(qusetion.getId());
                totalMark += qusetion.getMark();
                paperQuestionMapper.insert(paperQuestionKey);
            }
        }
        if(duoxuan!=null){
            criteria.clear();
            List<Integer> randomList = new ArrayList<Integer>();
            criteria.put("type",2);
            int total = questionMapper.countByExample(criteria);
            if(total<duoxuan){
                throw new RuntimeException("您设置的多选题数量大于数据库已有的！");
            }
            for(int i=0;i<duoxuan;i++){
                while (true){
                    Integer rand = (int)(Math.random()*total);
                    if(!randomList.contains(rand)){
                        randomList.add(rand);
                        break;
                    }
                }
            }
            Question qusetion = new Question();
            PaperQuestionKey paperQuestionKey = new PaperQuestionKey();
            paperQuestionKey.setPaperid(paper.getId());
            for(int t:randomList){
                criteria.clear();
                criteria.put("type",2);
                criteria.setMysqlOffset(t);
                criteria.setMysqlLength(1);
                qusetion = questionMapper.selectByExample(criteria).get(0);
                paperQuestionKey.setQuestionid(qusetion.getId());
                totalMark += qusetion.getMark();
                paperQuestionMapper.insert(paperQuestionKey);
            }
        }
        if(panduan!=null){
            criteria.clear();
            List<Integer> randomList = new ArrayList<Integer>();
            criteria.put("type",3);
            int total = questionMapper.countByExample(criteria);
            if(total<panduan){
                throw new RuntimeException("您设置的判断题数量大于数据库已有的！");
            }
            for(int i=0;i<panduan;i++){
                while (true){
                    Integer rand = (int)(Math.random()*total);
                    if(!randomList.contains(rand)){
                        randomList.add(rand);
                        break;
                    }
                }
            }
            Question qusetion = new Question();
            PaperQuestionKey paperQuestionKey = new PaperQuestionKey();
            paperQuestionKey.setPaperid(paper.getId());
            for(int t:randomList){
                criteria.clear();
                criteria.put("type",3);
                criteria.setMysqlOffset(t);
                criteria.setMysqlLength(1);
                qusetion = questionMapper.selectByExample(criteria).get(0);
                paperQuestionKey.setQuestionid(qusetion.getId());
                totalMark += qusetion.getMark();
                paperQuestionMapper.insert(paperQuestionKey);
            }
        }
        if(wordCaozuo!=null){
            criteria.clear();
            List<Integer> randomList = new ArrayList<Integer>();
            criteria.put("type",4);
            int total = questionMapper.countByExample(criteria);
            if(total<wordCaozuo){
                throw new RuntimeException("您设置的word操作题数量大于数据库已有的！");
            }
            for(int i=0;i<wordCaozuo;i++){
                while (true){
                    Integer rand = (int)(Math.random()*total);
                    if(!randomList.contains(rand)){
                        randomList.add(rand);
                        break;
                    }
                }
            }
            Question qusetion = new Question();
            PaperQuestionKey paperQuestionKey = new PaperQuestionKey();
            paperQuestionKey.setPaperid(paper.getId());
            for(int t:randomList){
                criteria.clear();
                criteria.put("type",4);
                criteria.setMysqlOffset(t);
                criteria.setMysqlLength(1);
                qusetion = questionMapper.selectByExample(criteria).get(0);
                paperQuestionKey.setQuestionid(qusetion.getId());
                totalMark += qusetion.getMark();
                paperQuestionMapper.insert(paperQuestionKey);
            }
        }
        if(excelCaozuo!=null){
            criteria.clear();
            List<Integer> randomList = new ArrayList<Integer>();
            criteria.put("type",5);
            int total = questionMapper.countByExample(criteria);
            if(total<excelCaozuo){
                throw new RuntimeException("您设置的excel操作题数量大于数据库已有的！");
            }
            for(int i=0;i<excelCaozuo;i++){
                while (true){
                    Integer rand = (int)(Math.random()*total);
                    if(!randomList.contains(rand)){
                        randomList.add(rand);
                        break;
                    }
                }
            }
            Question qusetion = new Question();
            PaperQuestionKey paperQuestionKey = new PaperQuestionKey();
            paperQuestionKey.setPaperid(paper.getId());
            for(int t:randomList){
                criteria.clear();
                criteria.put("type",5);
                criteria.setMysqlOffset(t);
                criteria.setMysqlLength(1);
                qusetion = questionMapper.selectByExample(criteria).get(0);
                paperQuestionKey.setQuestionid(qusetion.getId());
                totalMark += qusetion.getMark();
                paperQuestionMapper.insert(paperQuestionKey);
            }
        }
        if(powerpointCaozuo!=null){
            criteria.clear();
            List<Integer> randomList = new ArrayList<Integer>();
            criteria.put("type",6);
            int total = questionMapper.countByExample(criteria);
            if(total<powerpointCaozuo){
                throw new RuntimeException("您设置的excel操作题数量大于数据库已有的！");
            }
            for(int i=0;i<powerpointCaozuo;i++){
                while (true){
                    Integer rand = (int)(Math.random()*total);
                    if(!randomList.contains(rand)){
                        randomList.add(rand);
                        break;
                    }
                }
            }
            Question qusetion = new Question();
            PaperQuestionKey paperQuestionKey = new PaperQuestionKey();
            paperQuestionKey.setPaperid(paper.getId());
            for(int t:randomList){
                criteria.clear();
                criteria.put("type",6);
                criteria.setMysqlOffset(t);
                criteria.setMysqlLength(1);
                qusetion = questionMapper.selectByExample(criteria).get(0);
                paperQuestionKey.setQuestionid(qusetion.getId());
                totalMark += qusetion.getMark();
                paperQuestionMapper.insert(paperQuestionKey);
            }
        }
        if(dazi!=null){
            if(dazi>1){
                throw new RuntimeException("打字题最多只允许设置一个！");
            }
            criteria.clear();
            criteria.put("type",7);
            int total = questionMapper.countByExample(criteria);
            if(total<=0){
                throw new RuntimeException("数据库里没有打字题！");
            }
            Integer rand = (int)(Math.random()*total);
            criteria.setMysqlOffset(rand);
            criteria.setMysqlLength(1);
            Question qusetion = questionMapper.selectByExample(criteria).get(0);
            PaperQuestionKey paperQuestionKey = new PaperQuestionKey();
            paperQuestionKey.setPaperid(paper.getId());
            paperQuestionKey.setQuestionid(qusetion.getId());
            totalMark += qusetion.getMark();
            paperQuestionMapper.insert(paperQuestionKey);
        }
        return 1;
    }


    /**
     * 查找考生能够参加的考试
     * @param criteria
     * @return
     */
    @Transactional(readOnly = true)
    public List getPaperforKs(Criteria criteria){
        return paperMapper.getPaperforKs(criteria);
    }

    /**
     * 查找考生能够查看的考试
     * @param criteria
     * @return
     */
    @Transactional(readOnly = true)
    public List getPaperforLook(Criteria criteria){
        return paperMapper.getPaperforLook(criteria);
    }
}